package com.lcy.web.resource;

import com.lcy.web.utils.CommonUtils;

import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;

/**
 * Created by loucunyin on 2017/1/4 0004.
 * 读取 resources 中的 String
 */
public class StringResource {
    private ResourceBundle mBundle;

    /**
     * @param path 文件在 resources 中的路径
     */
    public StringResource(String path) {
        mBundle = ResourceBundle.getBundle(path);
    }

    /**
     * 根据 key 获取 value
     *
     * @param key key
     * @return string
     */
    public String getString(String key) {
        return getString(key, null);
    }

    public String getString(String id, String defaultValue) {
        String str = mBundle.getString(id);
        if (CommonUtils.checkString(str)) {
            String string = null;
            try {
                string = new String(str.getBytes("ISO-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return string;
        }
        return defaultValue;
    }
}
